/** 
* @Title: CallableAndFuture.java 
* @Package com.lh.threadtest.t9 
* @Description: TODO
* @author Liu 
* @date 2018年1月17日 下午3:28:19 
* @version V1.0 
*/
package com.lh.threadtest.t9;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/** 
* @ClassName: CallableAndFuture 
* @Description: Callable与Future的应用
* 
* 将一个有返回值的任务交给线程池，并等待线程池执行完毕返回结果...
* 
* @author Liu
* @date 2018年1月17日 下午3:28:19 
*  
*/
public class CallableAndFuture {

	/***
	* @Title: main 
	* @Description: TODO
	* @param @param args
	* @return void
	* @throws 
	*/
	public static void main(String[] args) {
		ExecutorService executorService = Executors.newSingleThreadExecutor();
		Future<String> future = executorService.submit(new Callable<String>() {
			/* (非 Javadoc) 
			* <p>Title: call</p> 
			* <p>Description: </p> 
			* @return
			* @throws Exception 
			* @see java.util.concurrent.Callable#call() 
			*/
			@Override
			public String call() throws Exception {
				TimeUnit.SECONDS.sleep(3);
				return "hello";
			}
		});
		
		System.out.println("waiting...");
		try {
			System.out.println(future.get());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
